<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<script type="text/javascript"
	src="${pageContext.request.contextPath }/assets/js/jquery-1.9.1.js"></script>
<body>
	<!-- 新增产品 -->
	<%-- <h3>十大热销产品</h3>
	<form action="${pageContext.request.contextPath }/saveProductInfo.do"
		onsubmit="checkForm()" method="post" enctype="multipart/form-data">
		产品名称<input name="productName"> <br> 产品价格<input
			name="price"> <br> 产品类别<select id="productTypeSelect">
		</select> <br> 产品图片<input type="file" name="file"> <br> 产品介绍<input
			name="introduction"> <br> <input type="submit"
			id="addBtn" value="添加"> <input type="hidden"
			name="productTypeId" id="productTypeId">
	</form> --%>



	<!-- 新增产品 -->
	<h3>产品添加，多图上传</h3>
	<form name="userForm2"
		action="${pageContext.request.contextPath }/saveMultipartProductInfo.do" onsubmit="checkForm()"
		enctype="multipart/form-data" method="post" >
		产品名称<input name="productName"> <br> 产品价格<input
			name="price"> <br> 产品类别<select id="productTypeSelect">
		</select> <br> 产品图片<input type="file" name="productFile"> <br> 产品介绍<input
			name="introduction"> <br> <input type="hidden"
			name="productTypeId" id="productTypeId">
		<h4>产品描述图片</h4>
		<div id="newUpload2">
			<input type="file" name="file">
		</div>
		<input type="button" id="btn_add2" value="增加一行"> <input
			type="submit" value="上传">

	</form>
	<script type="text/javascript">
		$(document)
				.ready(
						function() {
							//初始化产品类型下拉列表
							var url = "${pageContext.request.contextPath}/listProducttype.do"
							var data = {
								isDeleted : 0
							};
							$
									.post(
											url,
											data,
											function(resp) {
												//alert(JSON.stringify(resp));
												var productTypeList = resp.rows;

												for (var i = 0; i < productTypeList.length; i++) {
													var product = productTypeList[i];
													if (i == 0) {
														$("#productTypeId")
																.val(product.id);
													}
													$("#productTypeSelect")
															.append(
																	"<option value='"+product.id+"'>"
																			+ product.productTypeName
																			+ "</option>")
												}

											}, 'json');
							var j = 0;
							$("#btn_add2")
									.click(
											function() {
												document
														.getElementById("newUpload2").innerHTML += '<div id="div_'+j+'"><input  name="file_'+j+'" type="file"  /><input type="button" value="删除"  onclick="del_2('
														+ j + ')"/></div>';
												j = j + 1;
											});

							//添加产品
							/* $("#addBtn").click(function(){
								var productTypeId  = $('#productTypeSelect option:selected').val();
								$("#productTypeId").val(productTypeId);
								var data = $("form").serialize();
								var url = "${pageContext.request.contextPath }/product/addProduct.do";
								$.post(url,data,function(resp){
									alert(resp);	
								},'json');
							}); */

							

						});
		
		function checkForm() {
			//TODO 需对表单进行一些验证
			var productTypeId = $(
					'#productTypeSelect option:selected')
					.val();
			$("#productTypeId").val(productTypeId);
			return true;

		}
		
		 function del_2(o){  
	         document.getElementById("newUpload2").removeChild(document.getElementById("div_"+o));  
	    } 
	</script>
</body>
</html>